package com.newtouch.cq.base.starter.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newtouch.cq.base.starter.business.entity.SysMenu;
import com.newtouch.cq.base.starter.business.entity.SysUser;
import com.newtouch.cq.base.starter.business.mapper.BaseSysMenuMapper;
import com.newtouch.cq.base.starter.business.model.dto.MenuTree;
import com.newtouch.cq.base.starter.business.model.dto.SysMenuAddDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysMenuUpdDTO;
import com.newtouch.cq.base.starter.business.model.vo.MenuVO;
import com.newtouch.cq.base.starter.business.model.vo.UserPermissionInfoModel;
import com.newtouch.cq.base.starter.business.service.IBaseSysMenuService;
import com.newtouch.cq.base.starter.business.service.IBaseSysUserService;
import com.newtouch.cq.base.starter.lang.util.CurrentUserUtil;
import com.newtouch.cq.base.starter.lang.util.TreeUtil;
import com.newtouch.cq.lang.em.StatusEnum;
import com.newtouch.cq.lang.result.MccObjectResult;
import com.newtouch.cq.lang.util.DateUtils;
import com.newtouch.cq.mybatis.util.DbUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@Service
public class BaseSysMenuServiceImpl extends ServiceImpl<BaseSysMenuMapper, SysMenu> implements IBaseSysMenuService {


    @Resource
    private IBaseSysUserService userService;

    private Log log = LogFactory.get(BaseSysMenuServiceImpl.class);

    @Override
    public MccObjectResult<UserPermissionInfoModel> getUserPermissionInfo() {
        long userId = CurrentUserUtil.userIdOrThrow();
        List<MenuVO> menuList = selectUserMenu(userId);
//        List<SysRoleVo> roleList = userService.selectUserRoleList(user.getId());
        SysUser sysUser = userService.getById(userId);
        UserPermissionInfoModel permissionInfo = new UserPermissionInfoModel(sysUser, menuList, null);
        return MccObjectResult.success(permissionInfo);
    }

    @Override
    public List<MenuVO> selectUserMenu(long userId) {
        return baseMapper.getByUserId(userId);
    }

    @Override
    public List<MenuTree> getTree() {
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery(SysMenu.class)
                .ne(SysMenu::getStatus, StatusEnum.DELETE.getValue());
        List<SysMenu> menuTreeList = baseMapper.selectList(wrapper);
        menuTreeList.sort(Comparator.comparing(e -> e.getSort()));
        return TreeUtil.bulidTree(menuTreeList, -1);
    }

    @Override
    public List<MenuTree> allTreeCurrentUser() {
        long userId = CurrentUserUtil.userIdOrThrow();
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery(SysMenu.class)
                .eq(SysMenu::getStatus, StatusEnum.VALID.getValue());
        List<SysMenu> sysMenus = baseMapper.selectList(wrapper);
        return TreeUtil.bulidTree(sysMenus,-1);
    }

    @Override
    public void add(SysMenuAddDTO req) {
        long operatorId = CurrentUserUtil.userIdOrThrow();

        SysMenu menu = new SysMenu();
        BeanUtil.copyProperties(req, menu, CopyOptions.create().ignoreNullValue());
        menu.setStatus(StatusEnum.VALID);
        menu.setCreateId(operatorId);
        menu.setCreateTime(DateUtils.localDateTime());
        menu.setUpdateId(operatorId);
        menu.setUpdateTime(menu.getCreateTime());
        baseMapper.insert(menu);
    }

    @Override
    public MenuVO selectById(long id) {
        SysMenu sysMenu = baseMapper.selectById(id);
        return ObjectUtil.isNull(sysMenu) ? null
                : JSONUtil.toBean(JSONUtil.toJsonStr(sysMenu), MenuVO.class);
    }

    @Override
    public void upadte(long id,SysMenuUpdDTO req) {
        long operatorId = CurrentUserUtil.userIdOrThrow();
        DbUtil.containId(baseMapper,id,log);
        SysMenu menu = new SysMenu();
        BeanUtil.copyProperties(req, menu, CopyOptions.create().ignoreNullValue());

        menu.setId(id);
        menu.setUpdateId(operatorId);
        menu.setUpdateTime(DateUtils.localDateTime());
        baseMapper.updateById(menu);
    }

    @Override
    public void delete(long id) {
        long operatorId = CurrentUserUtil.userIdOrThrow();
        SysMenu menu = new SysMenu();
        menu.setId(id);
        menu.setStatus(StatusEnum.DELETE);
        menu.setUpdateId(operatorId);
        menu.setUpdateTime(DateUtils.localDateTime());
        baseMapper.updateById(menu);
    }

    @Override
    public List<SysMenu> getRolePermissions(long roleId) {
        return baseMapper.getRolePermissions(roleId);
    }
}
